//
//  ScrollLabel.m
//  SpeedLife
//
//  Created by 辰领科技 on 16/2/29.
//  Copyright © 2016年 yuf. All rights reserved.
//

#import "ScrollLabel.h"

@interface ScrollLabel ()
@property(nonatomic,strong)NSArray * titleArray;
@property(nonatomic,strong)void(^complect)(NSInteger index);
@property(nonatomic,assign)NSInteger scroIndex;
@property(nonatomic,strong)UILabel * scroLabel;
@property(nonatomic,assign)NSInteger duration;
@end
@implementation ScrollLabel

- (instancetype)initWithFrame:(CGRect)frame TitleArray:(NSArray *)titleArray duration:(NSInteger)duration Complect:(void(^)(NSInteger index))complect{
    self = [super initWithFrame:frame];
    if (self) {
        _titleArray = titleArray;
        _scroIndex = 0;
        _complect = complect;
        _duration = duration;
        self.backgroundColor = [UIColor colorWithRed:1.000 green:0.969 blue:0.882 alpha:1.000];
       
        _scroLabel = [[UILabel alloc]init];
        if(titleArray.count>0){
            _scroLabel.text = titleArray[0];
        }
         CGSize strSize = [_scroLabel.text sizeWithFont:[UIFont systemFontOfSize:14*VerticalTexeRatio()]];
        _scroLabel.frame = flexibleFrame(CGRectMake(frame.size.width, 0, strSize.width, frame.size.height), YES);
        _scroLabel.textColor = [UIColor colorWithRed:0.706 green:0.294 blue:0.059 alpha:1.000];
        
        _scroLabel.font = [UIFont systemFontOfSize:14*VerticalTexeRatio()];
        [self addSubview:_scroLabel];
        
        UIView * maskView =[[UIView alloc]initWithFrame:flexibleFrame(CGRectMake(0, 0, 37, frame.size.height-1), YES)];
        maskView.backgroundColor = [UIColor colorWithRed:1.000 green:0.969 blue:0.882 alpha:1.000];
        [self addSubview:maskView];
        
        UIView * line = [[UIView alloc]initWithFrame:flexibleFrame(CGRectMake(0, frame.size.height-1, frame.size.width, 1), YES)];
        line.backgroundColor = [UIColor colorWithRed:1.000 green:0.929 blue:0.812 alpha:1.000];
        [self addSubview:line];
        
        UIImageView * image = [[UIImageView alloc]initWithFrame:flexibleFrame(CGRectMake(12, 12, 16, 13), YES)];
        image.image = [UIImage imageNamed:@"home1"];
        [self addSubview:image];
        
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = flexibleFrame(CGRectMake(0, 0, frame.size.width, frame.size.height), YES);
        [button addTarget:self action:@selector(actionHandle) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        
    }
    return self;
}

- (void)startScroll{
    [UIView animateWithDuration:_duration animations:^{
        _scroLabel.frame = flexibleFrame(CGRectMake(-_scroLabel.frame.size.width, 0, _scroLabel.frame.size.width, self.frame.size.height), YES);
        }completion:^(BOOL finished) {
            
            if (_scroIndex<_titleArray.count-1) {
                _scroIndex ++;
            }else{
                _scroIndex = 0;
            }
            if(_titleArray.count>0){
                _scroLabel.text = _titleArray[_scroIndex];
            }
            
            CGSize strSize = [_scroLabel.text sizeWithFont:[UIFont systemFontOfSize:14*VerticalTexeRatio()]];
            _scroLabel.frame = flexibleFrame(CGRectMake(self.frame.size.width, 0, strSize.width, self.frame.size.height), YES);
            [self startScroll];
        }];
}

- (void)actionHandle{
    _complect(_scroIndex);
}

@end
